package com.zhangjun.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

/**
 * @author 张钧
 * @Description
 * @create 2022-10-14 10:54
 */
@Configuration
@MapperScan("com.zhangjun.mapper")
public class MybatisConfiguration {

	@Bean
	public SqlSessionFactoryBean sqlSessionFactoryBean(DruidDataSource druidDataSource) throws IOException {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
		sqlSessionFactoryBean.setDataSource(druidDataSource);
		sqlSessionFactoryBean.setTypeAliasesPackage("com.zhangjun.entity");
		sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
		return sqlSessionFactoryBean;
	}
}
